package com.qingzhou.web.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.qingzhou.web.repository.dao.PatientDao;
import com.qingzhou.web.repository.entity.*;
import com.qingzhou.web.service.impl.params.vo.*;
import com.qingzhou.web.service.util.DictUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ConvertHelper {
    @Inject
    PatientDao patientDao;
    public Page<CollectVo> convertCollect(Page<CollectEntity> pageResult) {
        List<CollectEntity> records = pageResult.getRecords();
        Page<CollectVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(records)){
            List<CollectVo> voList = new ArrayList<>();
            List<String> patientCodes = records.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            records.forEach(item->{
                CollectVo vo = BeanUtil.copyProperties(item, CollectVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }
    public Page<OmicsCollectVo> convertOmicsCollect(Page<OmicsCollectEntity> pageResult) {
        List<OmicsCollectEntity> records = pageResult.getRecords();
        Page<OmicsCollectVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(records)){
            List<OmicsCollectVo> voList = new ArrayList<>();
            List<String> patientCodes = records.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            records.forEach(item->{
                OmicsCollectVo vo = BeanUtil.copyProperties(item, OmicsCollectVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }


    public Page<CheckRecordsVo> convertCheckRecords(Page<CheckRecordsEntity> pageResult) {
        List<CheckRecordsEntity> records = pageResult.getRecords();
        Page<CheckRecordsVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(records)){
            List<CheckRecordsVo> voList = new ArrayList<>();
            List<String> patientCodes = new ArrayList<>(records.stream().map(item -> item.getPatientCode()).collect(Collectors.toSet()));
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            records.forEach(item->{
                CheckRecordsVo vo = BeanUtil.copyProperties(item, CheckRecordsVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public Page<ImagingVo> convertImaging(Page<ImagingEntity> pageResult) {
        List<ImagingEntity> entityList = pageResult.getRecords();
        Page<ImagingVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<ImagingVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                ImagingVo vo = BeanUtil.copyProperties(item, ImagingVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }


    public CollectVo convert(CollectEntity entity) {
        CollectVo vo = BeanUtil.copyProperties(entity, CollectVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }
    public OmicsCollectVo convert(OmicsCollectEntity entity) {
        OmicsCollectVo vo = BeanUtil.copyProperties(entity, OmicsCollectVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }

    public SurgeryVo convert(SurgeryEntity entity) {
        SurgeryVo vo = BeanUtil.copyProperties(entity, SurgeryVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }

    public CheckRecordsVo convert(CheckRecordsEntity entity) {
        CheckRecordsVo vo = BeanUtil.copyProperties(entity, CheckRecordsVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }

    public ImagingVo convert(ImagingEntity entity) {
        ImagingVo vo = BeanUtil.copyProperties(entity, ImagingVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }


    public Page<TreatmentVo> convertTreatment(Page<TreatmentEntity> pageResult) {
        List<TreatmentEntity> entityList = pageResult.getRecords();
        Page<TreatmentVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<TreatmentVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                TreatmentVo vo = BeanUtil.copyProperties(item, TreatmentVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public TreatmentVo convert(TreatmentEntity entity) {
        TreatmentVo vo = BeanUtil.copyProperties(entity, TreatmentVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }

    public Page<VisitVo> convertVisit(Page<VisitEntity> pageResult) {
        List<VisitEntity> entityList = pageResult.getRecords();
        Page<VisitVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<VisitVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                VisitVo vo = BeanUtil.copyProperties(item, VisitVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public VisitVo convert(VisitEntity entity) {
        VisitVo vo = BeanUtil.copyProperties(entity, VisitVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongingGroup(patient.getBelongingGroup());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }
    public Page<PathologyVo> convertPathology(Page<PathologyEntity> pageResult) {
        List<PathologyEntity> entityList = pageResult.getRecords();
        Page<PathologyVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<PathologyVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                PathologyVo vo = BeanUtil.copyProperties(item, PathologyVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public PathologyVo convert(PathologyEntity entity) {
        PathologyVo vo = BeanUtil.copyProperties(entity, PathologyVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }
    public Page<OmicsVo> convertOmics(Page<OmicsEntity> pageResult) {
        List<OmicsEntity> entityList = pageResult.getRecords();
        Page<OmicsVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<OmicsVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                OmicsVo vo = BeanUtil.copyProperties(item, OmicsVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public Page<SurgeryVo> convertSurgery(Page<SurgeryEntity> pageResult) {
        List<SurgeryEntity> entityList = pageResult.getRecords();
        Page<SurgeryVo> voPage = new Page<>();
        BeanUtil.copyProperties(pageResult,voPage,"records");
        if(CollUtil.isNotEmpty(entityList)){
            List<SurgeryVo> voList = new ArrayList<>();
            List<String> patientCodes = entityList.stream().map(item -> item.getPatientCode()).collect(Collectors.toList());
            Map<String, PatientEntity> patientMap = patientDao.selectMapByPatientCodes(patientCodes);
            entityList.forEach(item->{
                SurgeryVo vo = BeanUtil.copyProperties(item, SurgeryVo.class);
                PatientEntity patient = patientMap.get(vo.getPatientCode());
                if(ObjUtil.isNotNull(patient)){
                    vo.setHospitalSite(patient.getHospitalSite());
                    vo.setPatientName(patient.getPatientName());
                    vo.setTestItem(patient.getTestItem());
                    vo.setBeHospitalNum(patient.getBeHospitalNum());
                    vo.setBelongQueue(patient.getBelongQueue());
                    vo.setBelongQueue(patient.getBelongQueue());
                }
                voList.add(vo);
            });
            voPage.setRecords(voList);
        }
        return voPage;
    }

    public OmicsVo convert(OmicsEntity entity) {
        OmicsVo vo = BeanUtil.copyProperties(entity, OmicsVo.class);
        String patientCode = entity.getPatientCode();
        if(StrUtil.isNotEmpty(patientCode)){
            PatientEntity patient = patientDao.selectOneByPatientCode(patientCode);
            if(ObjUtil.isNotNull(patient)){
                vo.setHospitalSite(patient.getHospitalSite());
                vo.setPatientName(patient.getPatientName());
                vo.setTestItem(patient.getTestItem());
                vo.setBeHospitalNum(patient.getBeHospitalNum());
                vo.setBelongQueue(patient.getBelongQueue());
            }
        }
        return vo;
    }


}
